package com.zjitc.service.Imp;

import com.zjitc.mapper.CountryMapper;
import com.zjitc.module.Country;
import com.zjitc.module.Page;
import com.zjitc.service.CountryService;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 15:34
 * @description:
 */
@Service
public class CountryServiceImp implements CountryService {

  @Autowired
  private CountryMapper mapper;

  @Override
  public List<Country> findAll() {
    List<Country> all = mapper.findAll();
    return all;
  }

  @Override
  public List<Country> searchCountry(String region) {
    region = "%" + region + "%";
    List<Country> list = mapper.searchCountry(region);
    return list;
  }

  @Override
  public void add(Country country) {
    int a = mapper.add(country);
    System.out.println(a);
  }

  @Override
  public void update(Country country) {
    int up = mapper.update(country);
    System.out.println(up);
  }

  @Override
  public int delete(String code) {
    return mapper.delete(code);
  }

  @Override
  public Country findbycode(String code) {
    return mapper.findbycode(code);
  }

  //分页开始
  @Override
  public Page findbypage(int pageid, int count) {
    List<Country> countryList = mapper.findpage(pageid * count, count);
    Page page = new Page();
    Integer total = (int) Math.ceil(mapper.findAll().size() / count);

    int begin = pageid - 3;
    int end  = pageid + 3;
    while(begin < 0){
      begin ++;
      end ++;
    }
    while (end > total){
      end --;
      begin --;
    }
    page.setCountryList(countryList);
    page.setPageid(pageid);
    page.setTotal(total);
    page.setBegin(pageid);
    page.setBegin(begin);
    page.setEnd(end);
    return page;
  }

  @Override
  public Page pagesearch(String region) {
    Page page = new Page();
    page.setCountryList(mapper.searchCountry(region));
    page.setBegin(0);
    page.setEnd(0);
    page.setPageid(0);
    page.setTotal(0);
    return page;
  }

}
